package cn.cdeden.resource.dubbo;

import cn.cdeden.common.core.utils.StringUtils;
import cn.cdeden.common.oss.enumd.OssBucketOneEnum;
import cn.cdeden.common.oss.enumd.OssModuleOperate;
import cn.cdeden.common.oss.enumd.OssModuleType;
import cn.cdeden.resource.api.OssUploadService;
import cn.cdeden.resource.api.RemoteFileService;
import cn.cdeden.resource.api.domain.*;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @ProjectName : KnowCore01
 * @Package : cn.cdeden.resource.dubbo
 * @ClassName : OssUploadServiceImpl
 * @Author : Yu.
 * @Description : // 临时写入数据库
 * @Date : 2024/12/4 17:48
 * @Version : 1.0
 */

@Slf4j
@Service
@RequiredArgsConstructor
@DubboService(group = "temporary" ,version = "1.0.0")
public class OssUploadTemporaryServiceAImpl implements OssUploadService {


    @DubboReference(check = false)
    private final RemoteFileService fileService;

    @Override
    public RemoteFile getFileDataInfo(String moduleId, Long userId, JSONObject info, OssModuleOperate operate) {
        RemoteFile file = fileService.selectById(moduleId);
        if (ObjectUtil.isEmpty(file)){
            throw new RuntimeException("文件不存在");
        }
        if (!file.getModuleType().equals(OssModuleType.Temporary.getCode())) {
            throw new RuntimeException("文件类型错误");
        }
        return file;
    }

    @Override
    public RemoteOssUpload verifyData(RemoteOssUpload info, Long userId) {
        log.info("进入:temporary");
        String storageId = info.getFileId();
        if (StringUtils.isNotBlank(storageId)) {
            RemoteFile file = fileService.selectById(storageId);
            if (ObjectUtil.isEmpty(file)){
                throw new RuntimeException("文件不存在");
            }
        }
        info.setBrandName(OssBucketOneEnum.TEMPORARY_FILES.getCode()); //设置 桶的名称
        return info;
    }

    @Override
    public RemoteFilesBucketPath upload(RemoteFilesUpload upload, Long userId) {

//        RemoteFile file = new RemoteFile();
//        file.setOssId(Long.valueOf(upload.getStorageId()));
//        file.setUrl(upload.getUrl());
//        file.setOriginalName(upload.getFileName());
        RemoteFile file = fileService.selectById(upload.getStorageId());
//        Map<String, Object> jsonObject = new HashMap<>();
//        jsonObject.put("url", upload.getUrl());
//        jsonObject.put("ossId", upload.getStorageId());
//        jsonObject.put("fileName", file.getName());
        return RemoteFilesBucketPath
            .of(OssBucketOneEnum.TEMPORARY_FILES, file)
            .setUserList(Arrays.asList(String.valueOf(userId)));
    }



    @Override
    public List<RemoteFilesDownload> downloadFile(List<String> idList, Long userId, JSONObject jsonObject) {
        String collect = idList.stream().collect(Collectors.joining(","));
        List<RemoteFilesDownload> filesTrees = new ArrayList<>();
        List<RemoteFile> files = fileService.selectByIds(collect);
        if (ObjectUtil.isEmpty(files)){
            throw new RuntimeException("文件不存在");
        }else {
            files.stream().forEach(file ->{
                if (!file.getModuleType().equals(OssModuleType.Temporary.getCode())) throw new RuntimeException("在你的请求中有错误文件类型");
            });
        }
        for (RemoteFile remoteFile : files) {
            RemoteFilesDownload download = new RemoteFilesDownload();
            String storageId = String.valueOf(remoteFile.getOssId());
            download.setStorageId(storageId);
            download.setFolder(false);
            //设置文件名称和后缀
            download.setFileSuffix(remoteFile.getFileSuffix());
            download.setFileName(remoteFile.getOriginalName());
            download.setFileSize(remoteFile.getFileSize());
//            download.setParentId("10");
            download.setDateId(storageId); //数据的id
            filesTrees.add(download);
        }

//        RemoteFilesDownload download1 = new RemoteFilesDownload();
//        download1.setFolder(true);
//        download1.setFileName("测试001");
//        download1.setDateId("10");
//        filesTrees.add(download1);
        return filesTrees;

    }

}
